package com.peach.inquiry.imsdk.tim.message;

import com.peach.inquiry.IM;
import com.tencent.imsdk.v2.V2TIMFaceElem;
import com.tencent.imsdk.v2.V2TIMImageElem;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMSoundElem;
import com.tencent.imsdk.v2.V2TIMTextElem;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MsgUtil {

    public static MsgBean V2TIMMessage2MessageBean(V2TIMMessage org) {

        final MsgBean msg = new MsgBean();

        if (org.getLocalCustomData() != null) {
            msg.customMsgID = org.getLocalCustomData();
        } else {
            msg.customMsgID = "";
        }

        msg.msgID = org.getMsgID();
        msg.timestamp = org.getTimestamp();
        msg.senderID = org.getSender();
        msg.nickName = org.getNickName();
        msg.userID = org.getUserID();
        msg.isSelf = org.isSelf();
        msg.isRead = org.isRead();
        msg.isPeerRead = org.isPeerRead();
        msg.status = org.getStatus();

        if (org.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_TEXT) {
            msg.elemType = MsgBean.MsgType.text;
            V2TIMTextElem v2TIMTextElem = org.getTextElem();
            msg.desc = v2TIMTextElem.getText();
            msg.textElem = v2TIMTextElem.getText();
        } else if (org.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_CUSTOM) {
            msg.elemType = MsgBean.MsgType.custom;
            msg.desc = "[自定义消息]";
            msg.customElem = new String(org.getCustomElem().getData());
        } else if (org.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE) {
            msg.elemType = MsgBean.MsgType.image;
            V2TIMImageElem v2TIMImageElem = org.getImageElem();
            msg.desc = "[图片]";
            String imgPath = "";
            String thumbPath = "";
            int minDataSize = 0;
            int maxDataSize = 0;
//            List<MsgBean.ImgMsgContent.ImgContentItem> imgContentItems = new ArrayList<>();
            for (V2TIMImageElem.V2TIMImage v2TIMImage : v2TIMImageElem.getImageList()) {
//                MsgBean.ImgMsgContent.ImgContentItem imgContentItem = new MsgBean.ImgMsgContent.ImgContentItem();
//                imgContentItem.url = v2TIMImage.getUrl();
//                imgContentItem.size = v2TIMImage.getSize();
//                imgContentItem.uuid = v2TIMImage.getUUID();
//                imgContentItems.add(imgContentItem);
                int thisSize = v2TIMImage.getSize();
                if (thisSize < minDataSize) {
                    minDataSize = thisSize;
                    thumbPath = v2TIMImage.getUrl();
                } else if (thisSize > maxDataSize) {
                    maxDataSize = thisSize;
                    imgPath = v2TIMImage.getUrl();
                }
            }


            msg.thumbPath = thumbPath;
            msg.imgPath = imgPath;

            String localPath = org.getImageElem().getPath();
            if (localPath != null && !localPath.isEmpty() && new File(localPath).exists()) {
                msg.thumbPath = localPath;
                msg.imgPath = localPath;
            }
//            MsgBean.ImgMsgContent msgContent = new MsgBean.ImgMsgContent();
//            msgContent.imgList = imgContentItems;
//            msgContent.path = v2TIMImageElem.getPath();
//            msgContent.imgPath =
//                    msg.imageElem = msgContent;
        } else if (org.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_SOUND) {
            msg.elemType = MsgBean.MsgType.sound;
            msg.desc = "[语音]";
            V2TIMSoundElem orgSoundElem = org.getSoundElem();
//            final MsgBean.SoundMsgContent soundMsgContent = new MsgBean.SoundMsgContent();
//            soundMsgContent.dataSize = orgSoundElem.getDataSize();
//            soundMsgContent.path = orgSoundElem.getPath();
//            soundMsgContent.uuid = orgSoundElem.getUUID();
//            msg.soundElem = soundMsgContent;
            final String soundPath = IM.staticContext.getCacheDir() + File.separator + orgSoundElem.getUUID();
            File soundFile = new File(soundPath);
            if (soundFile.exists()) {
                if (soundFile.length() > 10) {
                    msg.soundPath = soundPath;
                }
            }
            msg.soundDuration = orgSoundElem.getDuration();

            //后面获取语音路径用
            IM.SoundElemListByMsgIDMap.put(msg.msgID, orgSoundElem);
        } else if (org.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_FACE) {
            msg.elemType = MsgBean.MsgType.face;
            msg.desc = "[表情]";
            V2TIMFaceElem orgFaceElem = org.getFaceElem();
            msg.customElem = new String(orgFaceElem.getData());
        } else {
            msg.elemType = MsgBean.MsgType.error;
            msg.desc = "[其它消息]";
        }


        return msg;
    }


    public static List<MsgBean> V2TIMMessageList2MessageBeanList(List<V2TIMMessage> orgList) {
        List<MsgBean> msgBeanList = new ArrayList<>();
        if (orgList == null || orgList.isEmpty()) return msgBeanList;
        for (V2TIMMessage msg : orgList) {
            msgBeanList.add(V2TIMMessage2MessageBean(msg));
        }
        return msgBeanList;
    }

}
